package com.fengdi.service.impl;

import com.fengdi.mapper.CategoryMapper;
import com.fengdi.pojo.Category;
import com.fengdi.pojo.Result;
import com.fengdi.service.serviceInterface.CategoryService;
import com.fengdi.utils.UserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@SuppressWarnings("all")
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * @param category : 新增的文章分类信息
     * @return 新增结果
     * @author FengDi
     * @Description 新增文章分类
     */
    @Override
    public Result<?> addCategory(Category category) {
        try{
            if(category.getCategoryAlias().isEmpty() || category.getCategoryName().isEmpty()) {
                return Result.error("分类名称或别名不能为空");
            }
            // 如果该分类已经存在，不予创建
            if(this.categoryMapper.searchCategory(category.getCategoryAlias()) != null && this.categoryMapper.searchCategory(category.getCategoryAlias()).equals(category.getCategoryName())) {
                return Result.error("该分类已经存在");
            }

            Integer id = UserThreadLocalUtil.getUserID();
            this.categoryMapper.insert(
                category.getCategoryName(),
                category.getCategoryAlias(),
                id,
                LocalDateTime.now().toString(),
                LocalDateTime.now().toString()
            );

            return Result.success();
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * @param userId : 当前用户ID值
     * @return 查询得到的文章列表结果
     * @author FengDi
     * @Description 获取当前用户的全部分类列表
     */
    @Override
    public Result<List<Category>> getCategories(int userId) {
        try {
            return Result.success(this.categoryMapper.selectAllCategoryByUserId(userId));
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * @param id : 分类ID
     * @return 分类详细信息
     * @author FengDi
     * @Description 获取分类详细信息
     */
    @Override
    public Result<Category> getCategoryDetails(Integer id) {
        try {
            return Result.success(this.categoryMapper.selectCategoryByCategoryId(id));
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * @param category : 更新数据
     * @return 更新结果
     * @author FengDi
     * @Description 更新文章分类
     */
    @Override
    public Result<?> updateCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        this.categoryMapper.updateCategoryById(category);
        return Result.success();
    }

    /**
     * @param id : 分类ID
     * @return 删除结果
     * @author FengDi
     * @Description 删除文章分类接口
     */
    @Override
    public Result<?> deleteCategory(Integer id) {
        this.categoryMapper.deleteCategoryById(id);
        return Result.success();
    }
}
